package com.feiyuan.movie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyuan.movie.dao.MovieJiListDao;
import com.feiyuan.movie.entity.MovieJiListEntity;
import com.feiyuan.movie.vo.MoviePlayerInfoVO;
import com.feiyuan.movie.vo.UserHistoryMovieVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyuan.common.utils.PageUtils;
import com.feiyuan.common.utils.Query;

import com.feiyuan.movie.dao.MovieUserHistoryDao;
import com.feiyuan.movie.entity.MovieUserHistoryEntity;
import com.feiyuan.movie.service.MovieUserHistoryService;


@Service("movieUserHistoryService")
public class MovieUserHistoryServiceImpl extends ServiceImpl<MovieUserHistoryDao, MovieUserHistoryEntity> implements MovieUserHistoryService {

    @Autowired
    private MovieJiListDao jiListDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MovieUserHistoryEntity> page = this.page(
                new Query<MovieUserHistoryEntity>().getPage(params),
                new QueryWrapper<MovieUserHistoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public IPage<UserHistoryMovieVO> pageHistory(Page<MovieUserHistoryEntity> userHistoryEntityPage, Long userId) {
        IPage<UserHistoryMovieVO> page = this.baseMapper.selectHistory(userHistoryEntityPage, userId);
        return page;
    }

    @Override
    public MoviePlayerInfoVO selectByUserHistoryId(Long userId, Long userHistoryId) {
        MoviePlayerInfoVO moviePlayerInfoVO = this.baseMapper.selectByUserHistoryId(userId, userHistoryId);

        // 查询集列表
        List<MovieJiListEntity> jiListEntities = jiListDao.selectJiList(moviePlayerInfoVO.getMovieId());
        moviePlayerInfoVO.setJiList(jiListEntities);
        if (StringUtils.isEmpty(moviePlayerInfoVO.getMovieJiUrl())) {
            MovieJiListEntity movieJiListEntity = jiListEntities.get(jiListEntities.size() - 1);
            moviePlayerInfoVO.setMovieJiUrl(movieJiListEntity.getMovieJiUrl());
            moviePlayerInfoVO.setMovieNowJi(Math.toIntExact(movieJiListEntity.getId()));
            moviePlayerInfoVO.setMovieNowSecond(0);
            moviePlayerInfoVO.setPlayName(movieJiListEntity.getPlayName());
        }

        return moviePlayerInfoVO;
    }

}